<?php

/**
 * @author clive
 */
abstract class view {

    protected $action = '';
    protected $tables = array(); //储存MYSQL返回的表数据(是一个带名字的数组)
    protected $assoc = array(); //储存一个带名字的数组
    protected $search = array(); //对于列表需要定义搜索数组
    protected $pageinfo = array(); //分页信息
    protected $order = array(); //排序

    public function __set($property, $value) {
        die('不能ASSIGN这个属性[' . $property . ']！因为在｛视图层｝CLASS类里面未定义！');
    }

    /*
      public function assign($name, $value) {
      if (!property_exists('view', $name)) {

      }
      $this->$name = $value;
      }
     */

    public function assigndata($data) {
        //功能loadProperty（载入属性）或者assigndata（赋值数据）
        //在VIEW层获取CTRL层的数据时用的（载入属性）
        if (0 && is_array($data)) {
            foreach ($data as $property => $value) {
                $this->$property = $value;
            }
        }
        //1：表格数据
        $this->tables = $data['tables'];
        $this->assoc = $data['assoc'];
        //2：搜索条件
        $this->search = $data['search'];
        //3：分页信息
        $this->pageinfo = $data['pageinfo'];
        $this->order = $data['order'];
    }

    public function display($tplname = '') {
        if (1) {
            $get = '';
            foreach ($_GET as $getkey => $getval) {
                if ($getval != '' && !is_array($getval))
                    $get.='&' . urlencode($getkey) . '=' . urlencode($getval);
            }
            $this->action = '?/' . $get;
        }
        if ($tplname == '') {
            $tplname = app::$CONTROLLER_NAME . '_' . app::$ACTION_NAME;
        }
        $files = array();
        $files[] = app::$APP_ROOTDIR . __DS__ . 'tpl' . __DS__ . app::$APP_VIEW . __DS__ . $tplname . '.php';
        $files[] = app::$APP_BASE . __DS__ . app::$APP_EXTEND . __DS__ . 'tpl' . __DS__ . app::$APP_VIEW . __DS__ . $tplname . '.php';
        $tplname = 'tpl' . __DS__ . $tplname;
        $files[] = app::$APP_ROOTDIR . __DS__ . 'view' . __DS__ . app::$APP_VIEW . __DS__ . $tplname . '.php';
        $files[] = app::$APP_BASE . __DS__ . app::$APP_EXTEND . __DS__ . 'view' . __DS__ . app::$APP_VIEW . __DS__ . $tplname . '.php';
        $file = getExistsFileByArr($files);
        if (is_readable($file)) {
            include $file;
        } else {
            echo '======= can not load tpl file =======';
            print_rr($files);
            die;
        }
    }

    /*
     * 给字段做排序功能，用在tpl里的table表列头的td标签里
     */

    function orderField($orderField) {
        $ret = " orderField=\"{$orderField}\"";
        $arr = explode(',', $this->order['Field']);
        $arr = explode(' ', $arr[0]);
        if ($arr[0] == $orderField) {
            $ret.=" class=\"{$this->order['Direction']}\"";
        }
        return($ret);
    }

    /*
     * 获取SELECT的选择列表（用在列表页面的分页里每页最多数量的选择）
     */

    function getOrderOption($numPerPage) {
        /*
         * 获取排序的SELECT选择项
         */
        $numPerPageList = "15,30,50,100,200,500";
        $arr = explode(',', $numPerPageList);
        $arr[] = $numPerPage;
        $arr = array_unique($arr); //删除重复项
        sort($arr); //从小到大的排序
        $str = '';
        foreach ($arr as $val) {
            $def = '';
            if ($numPerPage == $val) {
                $def = " selected=\"selected\"";
            }
            $str.="<option{$def} value=\"{$val}\">{$val}</option>";
        }
        return($str);
    }

}
